package com.hongxun.project.assets.iPicture.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import com.hongxun.framework.config.RuoYiConfig;
import com.hongxun.project.assets.bByq.domain.BByq;
import com.hongxun.project.assets.bByq.service.IBByqService;
import com.hongxun.project.assets.iPoint.domain.IPoint;
import com.hongxun.project.assets.iPoint.service.IIPointService;
import com.hongxun.project.system.user.domain.User;
import com.hongxun.project.system.user.service.IUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hongxun.framework.aspectj.lang.annotation.Log;
import com.hongxun.framework.aspectj.lang.enums.BusinessType;
import com.hongxun.project.assets.iPicture.domain.IPicture;
import com.hongxun.project.assets.iPicture.service.IIPictureService;
import com.hongxun.framework.web.controller.BaseController;
import com.hongxun.framework.web.page.TableDataInfo;
import com.hongxun.framework.web.domain.AjaxResult;
import com.hongxun.common.utils.poi.ExcelUtil;

import javax.servlet.http.HttpServletResponse;

/**
 * 现场照片 信息操作处理
 * 
 * @author hongxun
 * @date 2019-03-19
 */
@Controller
@RequestMapping("/assets/iPicture")
public class IPictureController extends BaseController
{
    private String prefix = "assets/iPicture";
	private String ROOT = RuoYiConfig.getProfile();

	@Autowired
	private IIPictureService iPictureService;
	@Autowired
	private IIPointService iPointService;
	@Autowired
	private IBByqService bByqService;
	@Autowired
	private IUserService userService;



	
	@RequiresPermissions("assets:iPicture:view")
	@GetMapping()
	public String iPicture()
	{
	    return prefix + "/iPicture";
	}

	/**
	 *
	 * @param byqId
	 * @param modelMap
	 * @return
	 * 2019-7-12 15:22:35
	 */
	@GetMapping("/iPictureShow/{byqId}")
	public String iPictureShow(@PathVariable("byqId") String byqId , ModelMap modelMap)
	{
		modelMap.put("byqId",byqId);
	    return prefix + "/iPictureShow";
	}
	
	/**
	 * 查询现场照片列表
	 */
	@RequiresPermissions("assets:iPicture:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(IPicture iPicture)
	{
		startPage();
        List<IPicture> list = iPictureService.selectIPictureList(iPicture);
		return getDataTable(list);
	}

	/**
	 * 	点位照片
	 *
	 * @param iPicture
	 * @return
	 * 2019-7-12 15:22:59
	 */
	@PostMapping("/listPhoto/{byqId}")
	@ResponseBody
	public TableDataInfo listPhoto(@PathVariable("byqId") String byqId , IPicture iPicture) throws ParseException {
		startPage();
		iPicture.setByqId(byqId);
		String tis = "";

//		if(iPicture.getTime() != null){
//			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//			tis = sdf.format(iPicture.getTime());
//			iPicture.setTime(sdf.parse(tis));
//		}
        List<IPicture> list = iPictureService.selectIPictureListForPoint(iPicture);
		//	点位和拍照人
		for (IPicture picture : list) {
			IPoint iPoint = iPointService.selectIPointById(picture.getPId());
			User user = userService.selectUserById(Long.parseLong(picture.getUserId()));
			picture.setpName(iPoint.getCode());
			if(user!=null) picture.setUserName(user.getUserName());else picture.setUserName("");
		}

		return getDataTable(list);
	}
	
	
	/**
	 * 导出现场照片列表
	 */
	@RequiresPermissions("assets:iPicture:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(IPicture iPicture)
    {
    	List<IPicture> list = iPictureService.selectIPictureList(iPicture);
        ExcelUtil<IPicture> util = new ExcelUtil<IPicture>(IPicture.class);
        return util.exportExcel(list, "iPicture");
    }
	
	/**
	 * 新增现场照片
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存现场照片
	 */
	@RequiresPermissions("assets:iPicture:add")
	@Log(title = "现场照片", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(IPicture iPicture)
	{		
		return toAjax(iPictureService.insertIPicture(iPicture));
	}

	/**
	 * 修改现场照片
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") String id, ModelMap mmap)
	{
		IPicture iPicture = iPictureService.selectIPictureById(id);
		mmap.put("iPicture", iPicture);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存现场照片
	 */
	@RequiresPermissions("assets:iPicture:edit")
	@Log(title = "现场照片", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(IPicture iPicture)
	{		
		return toAjax(iPictureService.updateIPicture(iPicture));
	}
	
	/**
	 * 删除现场照片
	 */
	@RequiresPermissions("assets:iPicture:remove")
	@Log(title = "现场照片", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(iPictureService.deleteIPictureByIds(ids));
	}


	/**
	 * 	照片显示
	 *
	 * @param iPicture
	 * @param response
	 * @throws IOException
	 */
	@GetMapping("getPicture")
	public void queryPic( IPicture iPicture, HttpServletResponse response) throws IOException {

		String address = iPicture.getAddress();
		if (address != null) {
			response.setContentType("image/jpeg");
			File file=new File(ROOT +   address);
			if(file.exists()) {
				FileInputStream is = new FileInputStream(ROOT + address);
				if (is != null) {
					int i = is.available(); // 得到文件大小
					byte data[] = new byte[i];
					is.read(data); // 读数据
					is.close();
					response.setContentType("image/jpeg");  // 设置返回的文件类型
					OutputStream toClient = response.getOutputStream(); // 得到向客户端输出二进制数据的对象
					toClient.write(data); // 输出数据
					toClient.close();
				}
			}
		}
	}

}
